package org.example.mapper;

import org.example.common.Status;
import org.example.entity.User;
import org.example.entity.dto.UserDto;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

@Mapper(componentModel = "spring", uses = {AddressMapper.class})
public interface UserMapper {

    UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

    @Mapping(source = "address", target = "address")
    @Mapping(source = "addresses", target = "addresses")
    @Mapping(source = "isActive", target = "status", qualifiedByName = "activeToStatus")
    // 指定了如果 User 对象中的 age 属性为 null，则默认值为 18。
    @Mapping(target = "age", defaultValue = "18")
    UserDto userToUserDto(User user);

    @Mapping(source = "address", target = "address")
    @Mapping(source = "addresses", target = "addresses")
    @Mapping(target = "isActive", source = "status", qualifiedByName = "statusToActive")
    User userDtoToUser(UserDto userDto);

    @Named("activeToStatus")
    default Status activeToStatus(Boolean isActive) {
        return isActive != null && isActive ? Status.ACTIVE : Status.INACTIVE;
    }

    @Named("statusToActive")
    default Boolean statusToActive(Status status) {
        return status == Status.ACTIVE;
    }
}